"""
Test Groups module 
"""

from django.test import TestCase
from RSM.my_friends.models import RSMUser, User
#from django.template import RequestContext
from RSM.my_friends.views import getUserNames
from RSM.util import getRSMGroup

getRSMGroup = getRSMGroup
getUserNames = getUserNames

class GroupsTest(TestCase):
    added = False
    testCount = 1
    
    def setUp(self):
        print "\nGroups Test" + str(GroupsTest.testCount) + "\n"
        if (GroupsTest.added):
            return
        #adding some users to the system
        user1 = User.objects.create_user("Tomer", "t@t.com", "1234")
        user1.save()
        RSMUser(user=user1).save()
        user2 = User.objects.create_user("Eran", "t@t.com", "1234") 
        user2.save()
        RSMUser(user=user2).save()
        user3 = User.objects.create_user("Alex", "t@t.com", "1234")
        user3.save()
        RSMUser(user=user3).save()

                
    def tearDown(self):
        print "\nEnd Groups Test" + str(GroupsTest.testCount) + "\n"
        GroupsTest.testCount += 1
        
    def test_1_basic(self):
        print "Adding users and checking initial state"
        #Checking init of test
        userNameList = getUserNames(RSMUser.objects.all())
        
        #Asserting users
        assert "Tomer" in userNameList
        assert "Eran" in userNameList
        assert "Alex" in userNameList
        assert "BlaBla" not in userNameList
        
        #Printing users
        for u in userNameList:
            print "Added RSMUser " + str(u) + " to the system"
        
        #Check no one has any friends
        users = RSMUser.objects.all()
        for u in (users):
            n1 = len(u.friends.all())
            n2 = len(u.groups.all())
            self.assertEquals(n1,0)
            self.assertEquals(n2,0)

        #Test Finnished
        print "Added users to the system, none have friends, none are members of groups"

        
    def test_2_addGroup(self):
        userNameList = getUserNames(RSMUser.objects.all())
        assert "Tomer" in userNameList
        self.client.login(password=u"1234",username="Tomer")
        print "Logged in as Tomer"
        self.client.post("/friends/activeAdd/", {"friendsToAdd":"Eran"})
        print "Added Eran as friend"
        #Adding a new group
        self.client.post("/groups/activeAdd/", {"groupname":"testGroup1"})
        print "added new group: testGroup1"
        #Check no one has any friends
        users = RSMUser.objects.all()
        for u in (users):
            n = len(u.groups.all())
            if (u.user.username != "Tomer"):
                self.assertEquals(n,0)
            else:
                self.assertEquals(n,1)
        print "Tomer has 1 groups, No one else has groups"        
    
    

    def test_3_AddFriendToGroup(self):
        userNameList = getUserNames(RSMUser.objects.all())
        assert "Tomer" in userNameList
        self.client.login(password=u"1234",username="Tomer")
        print "Logged in as Tomer"
        self.client.post("/friends/activeAdd/", {"friendsToAdd":"Eran"})
        print "Added Eran as friend"
        self.client.post("/groups/activeAdd/", {"groupname":"testGroup1"})
        print "added new group: testGroup1"
        self.client.post("/groups/editfriends/", {"friends":"Eran","action":"addFriend","groupname":"testGroup1"})
        print "added Eran to testGroup1"        

        #Check 
        users = RSMUser.objects.all()
        for u in (users):
            n = len(u.groups.all())
            if (u.user.username != "Tomer"):
                self.assertEquals(n,0)
            else:
                self.assertEquals(n,1)
                group = getRSMGroup("Tomer","testGroup1")
                self.assertEqual(len(group.members.all()),1)
        print "Tomer has one group that has one member"                
        
        
    def test_4_removeFriendFromGroup(self):
        userNameList = getUserNames(RSMUser.objects.all())
        assert "Tomer" in userNameList
        self.client.login(password=u"1234",username="Tomer")
        print "Logged in as Tomer"
        self.client.post("/friends/activeAdd/", {"friendsToAdd":"Eran"})
        print "Added Eran as friend"
        self.client.post("/groups/activeAdd/", {"groupname":"testGroup1"})
        print "added new group: testGroup1"
        self.client.post("/groups/editfriends/", {"friends":"Eran","action":"addFriend","groupname":"testGroup1"})
        print "added Eran to testGroup1"        
        self.client.post("/groups/editfriends/", {"friends":"Eran","action":"removeFriend","groupname":"testGroup1"})
        print "removed Eran from testGroup1"        

        #Check 
        users = RSMUser.objects.all()
        for u in (users):
            n = len(u.groups.all())
            if (u.user.username != "Tomer"):
                self.assertEquals(n,0)
            else:
                self.assertEquals(n,1)
                group = getRSMGroup("Tomer","testGroup1")
                self.assertEqual(len(group.members.all()),0)
        print "Tomer has one group that has no members"          

    def test_5_addMultipleFriendsToGroup(self):
        userNameList = getUserNames(RSMUser.objects.all())
        assert "Tomer" in userNameList
        self.client.login(password=u"1234",username="Tomer")
        print "Logged in as Tomer"
        self.client.post("/friends/activeAdd/", {"friendsToAdd":("Eran","Alex")})
        print "Added Eran & Alex as friends"
        self.client.post("/groups/activeAdd/", {"groupname":"testGroup1"})
        print "added new group: testGroup1"
        self.client.post("/groups/editfriends/", {"friends":("Eran","Alex"),"action":"addFriend","groupname":"testGroup1"})
        print "added Eran & Alex to testGroup1"        

        #Check 
        users = RSMUser.objects.all()
        for u in (users):
            n = len(u.groups.all())
            if (u.user.username != "Tomer"):
                self.assertEquals(n,0)
            else:
                self.assertEquals(n,1)
                group = getRSMGroup("Tomer","testGroup1")
                self.assertEqual(len(group.members.all()),2)
        print "Tomer has one group that has two members"   

        
    def test_6_removeMultipleFriendsFromGroup(self):
        userNameList = getUserNames(RSMUser.objects.all())
        assert "Tomer" in userNameList
        self.client.login(password=u"1234",username="Tomer")
        print "Logged in as Tomer"
        self.client.post("/friends/activeAdd/", {"friendsToAdd":("Eran","Alex")})
        print "Added Eran & Alex as friends"
        self.client.post("/groups/activeAdd/", {"groupname":"testGroup1"})
        print "added new group: testGroup1"
        self.client.post("/groups/editfriends/", {"friends":("Eran","Alex"),"action":"addFriend","groupname":"testGroup1"})
        print "added Eran & Alex to testGroup1"        
        self.client.post("/groups/editfriends/", {"friends":("Eran","Alex"),"action":"removeFriend","groupname":"testGroup1"})
        print "removed Eran & Alex from testGroup1"        

        #Check 
        users = RSMUser.objects.all()
        for u in (users):
            n = len(u.groups.all())
            if (u.user.username != "Tomer"):
                self.assertEquals(n,0)
            else:
                self.assertEquals(n,1)
                group = getRSMGroup("Tomer","testGroup1")
                self.assertEqual(len(group.members.all()),0)
        print "Tomer has one group that has no members" 
        
        
    def test_7_removeFriend(self):
        userNameList = getUserNames(RSMUser.objects.all())
        assert "Tomer" in userNameList
        self.client.login(password=u"1234",username="Tomer")
        print "Logged in as Tomer"
        self.client.post("/friends/activeAdd/", {"friendsToAdd":"Eran"})
        print "Added Eran as friend"
        self.client.post("/groups/activeAdd/", {"groupname":"testGroup1"})
        print "added new group: testGroup1"
        self.client.post("/groups/editfriends/", {"friends":"Eran","action":"addFriend","groupname":"testGroup1"})
        print "added Eran to testGroup1"        
        self.client.post("/friends/remove/", {"friendsList":"Eran"})
        print "Removed Eran from friends, should be removed from group"      

        #Check 
        users = RSMUser.objects.all()
        for u in (users):
            n = len(u.groups.all())
            if (u.user.username != "Tomer"):
                self.assertEquals(n,0)
            else:
                self.assertEquals(n,1)
                group = getRSMGroup("Tomer","testGroup1")
                self.assertEqual(len(group.members.all()),0)
        print "Tomer has one group that has no members"          
